import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;


public class JFrameClient extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField intervalTextField;
	private JButton btnUpdate;
	private JLabel lblcurrentInterval;
	private JLabel timeLabel;
	private JButton btnConnect;
	private JButton btnDisconnetc;

	/**
	 * Create the frame.
	 */
	public JFrameClient() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				ClientToServer.disconnect();
			}
		});
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		timeLabel = new JLabel("Current Time");
		timeLabel.setHorizontalAlignment(SwingConstants.CENTER);
		timeLabel.setFont(new Font("Dialog", Font.BOLD, 40));
		timeLabel.setBounds(52, 12, 356, 81);
		contentPane.add(timeLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Update Interval:");
		lblNewLabel_1.setFont(new Font("Dialog", Font.BOLD, 20));
		lblNewLabel_1.setBounds(28, 145, 186, 24);
		contentPane.add(lblNewLabel_1);
		
		intervalTextField = new JTextField();
		intervalTextField.setFont(new Font("Dialog", Font.PLAIN, 20));
		intervalTextField.setBounds(220, 145, 71, 27);
		contentPane.add(intervalTextField);
		intervalTextField.setColumns(10);
		
		btnUpdate = new JButton("Update");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int value = Integer.valueOf(intervalTextField.getText());
				ClientToServer.setInterval(value);
				lblcurrentInterval.setText("Current Update Intervale:	" + value + " s");
			}
		});

		btnUpdate.setBounds(311, 147, 117, 25);
		btnUpdate.setEnabled(false);
		contentPane.add(btnUpdate);
		
		lblcurrentInterval = new JLabel("Current Update Interval:	1 s");
		lblcurrentInterval.setFont(new Font("Dialog", Font.BOLD, 20));
		lblcurrentInterval.setBounds(28, 120, 356, 24);
		contentPane.add(lblcurrentInterval);
		
		btnDisconnetc = new JButton("Disconnect");
		btnDisconnetc.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				ClientToServer.disconnect();
				btnDisconnetc.setEnabled(false);
				btnUpdate.setEnabled(false);
				btnConnect.setEnabled(true);
			}
		});
		btnDisconnetc.setBounds(225, 205, 117, 25);
		btnDisconnetc.setEnabled(false);
		contentPane.add(btnDisconnetc);
		
		btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ClientToServer.registerInServer();
				btnConnect.setEnabled(false);
				btnDisconnetc.setEnabled(true);
				btnUpdate.setEnabled(true);
			}
		});
		btnConnect.setBounds(70, 205, 117, 25);
		contentPane.add(btnConnect);

	}
	
	public void setCurrentTime(String text){
		timeLabel.setText(text);
	}
}
